/**
 * 
 */
package MariMonDaily;

//import java.util.Date;
import java.util.List;

import org.joda.time.DateTime;

import com.dukascopy.api.IHistory;
import com.dukascopy.api.IOrder;
import com.dukascopy.api.Instrument;
import com.dukascopy.api.JFException;





/**
 * @author jon
 *
 */
public class TimeElapsedSinceLastOpenTrade {

	/**
	 * 
	 */
	private IOrder order;
	private IHistory history;
	
	public TimeElapsedSinceLastOpenTrade() {
		// TODO Auto-generated constructor stub
	}
	
	public long GetTimeElapsedOpen(String strSymbol, int nMagic, IOrder order, Instrument instrument, boolean BrokerHasSundayCandle, int DeltaTimeLiveTrades) throws JFException{
		
		long order_start = order.getFillTime();
		
		final long current_time = history.getTimeOfLastTick(Instrument.EURUSD);
		
		long timeelapsed = (order_start - current_time);
		
		int today = new DateTime().getDayOfWeek();
		int order_time_day = new DateTime(order_start).getDayOfWeek();
	
		if (BrokerHasSundayCandle == true){
		
			// If its Sunday then......
			if( today == 0  &&  order_time_day > 0 )  {
				
				timeelapsed = timeelapsed - 172800;
				
			}
		else{
			if ( today == 1  &&  order_time_day > 1) {
				
				timeelapsed = timeelapsed - 172800;
			}
		
		}
			// End of weeken case
		if (timeelapsed <= 0 ){
				timeelapsed = DeltaTimeLiveTrades + 1;
		}
		
	
	
		//return (timeelapsed);
		
	}
	
		return (timeelapsed);
	}
	
public long GetTimeElapsedClosed(String strSymbol, int nMagic, IOrder order, Instrument instrument, boolean BrokerHasSundayCandle, int DeltaTimeLiveTrades) throws JFException{
		
	// coresponds to "time elapsed since last histo trade
	
		long order_close = order.getCloseTime();
		
		final long current_time = history.getTimeOfLastTick(Instrument.EURUSD);
		
		long timeelapsed = (order_close - current_time);
		
		int today = new DateTime().getDayOfWeek();
		int order_time_day = new DateTime(order_close).getDayOfWeek();
	
		if (BrokerHasSundayCandle == true){
		
			// If its Sunday then......
			if( today == 0  &&  order_time_day > 0 )  {
				
				timeelapsed = timeelapsed - 172800;
				
			}
		else{
			if ( today == 1  &&  order_time_day > 1) {
				
				timeelapsed = timeelapsed - 172800;
			}
		
		}
			// End of weeken case
		if (timeelapsed <= 0 ){
				timeelapsed = DeltaTimeLiveTrades + 1;
		}
		
	
	
		//return (timeelapsed);
		
	}
	
		return (timeelapsed);
	}

	public long GetTimeElapsedSinceLastHistoricalBuyTrade(String strSymbol, int nMagic, List<IOrder> orders, Instrument instrument, boolean BrokerHasSundayCandle, int DeltaTimeLiveTrades) throws JFException{
		
		long time_elapsed = 0;
		
		
		// Loop through the current orders
		for (IOrder o : orders){
			if (o.isLong()){
				// if long, then do long stuff
				int today = new DateTime().getDayOfWeek();
				int order_time_day = new DateTime(o.getCloseTime()).getDayOfWeek();
				
				long timeelapsed = (history.getTimeOfLastTick(Instrument.EURUSD) - o.getCloseTime());
				
				if (BrokerHasSundayCandle == true){
				
					if (today == 0  &&  order_time_day > 0){
					
					timeelapsed = timeelapsed - 172800;
					
					}
				}
			else{
					if (today == 1  &&  order_time_day > 1){
					
					timeelapsed = timeelapsed - 172800;
					
					}
				
			}
				
	
			
				
			}
			
			
			else{
				// do short stuff
			}
		}
		
		
		return time_elapsed;
		
	
	}
	
public long GetTimeElapsedSinceLastHistoricalSellTrade(String strSymbol, int nMagic, List<IOrder> orders, Instrument instrument, boolean BrokerHasSundayCandle, int DeltaTimeLiveTrades) throws JFException{
		
		long time_elapsed = 0;
		
		
		// Loop through the current orders
		for (IOrder o : orders){
			if (! o.isLong()){
				// if short, then do short stuff
				int today = new DateTime().getDayOfWeek();
				int order_time_day = new DateTime(o.getCloseTime()).getDayOfWeek();
				
				long timeelapsed = (history.getTimeOfLastTick(Instrument.EURUSD) - o.getCloseTime());
				
				if (BrokerHasSundayCandle == true){
				
					if (today == 0  &&  order_time_day > 0){
					
					timeelapsed = timeelapsed - 172800;
					
					}
				}
			else{
					if (today == 1  &&  order_time_day > 1){
					
					timeelapsed = timeelapsed - 172800;
					
					}
				
			}
				

			
				
			}
			
			
			else{
				// do long stuff
			}
		}
		
		
		return time_elapsed;
		
	
	}
	
	
}

	

